﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Infrastructure.ObjectModel;
using Infrastructure.ServiceInterfaces;
using Infrastructure.Managers;

namespace B10_Ex05_Elad_Meyron.Models
{
    public class GameManager : GameService
    {
        #region Consts
        private const int k_numOfBasicDradles = 3;
        private const int k_numOfTexturedDradles = 3;
        #endregion

        #region Data members
        public int PlayerScore
        {
            get;
            set;
        }

        public DradleLetters PlayerGuess
        {
            get;
            set;
        }

        public bool IsGameOn
        {
            get;
            set;
        }

        public GameManager(Game i_game)
            : base(i_game)
        {
        }

        private Dradle[] m_arrDrls;
        private TexturedDradle[] m_arrTDrls;
        private int m_nCurrSpinning;
        private GameStatusDisplay m_Display;
        #endregion

        #region Methods
        /// <summary>
        /// Init method
        /// </summary>
        public override void Initialize()
        {
            // Init stuff
            base.Initialize();
            IsGameOn = false;
            m_nCurrSpinning = 0;
            SetScore(PlayerScore);
            InitPlayer();
        }

        /// <summary>
        /// Sets the player init stuff
        /// </summary>
        private void InitPlayer()
        {
            PlayerScore = 0;
            m_arrDrls = new Dradle[k_numOfBasicDradles];
            m_arrTDrls = new TexturedDradle[k_numOfTexturedDradles];
            PlayerGuess = DradleLetters.ERROR;
            
            // Create the new dradels
            for (int i = 0; i < k_numOfBasicDradles; i++)
            {
                m_arrDrls[i] = new Dradle(this.Game);
                m_arrDrls[i].DradleStoppedEvent += new DradleStoppedEventHandler(GameManager_DradleStoppedEvent);
            }
            
            // Create the new dradels
            for (int i = 0; i < k_numOfTexturedDradles; i++)
            {
                m_arrTDrls[i] = new TexturedDradle(this.Game);
                m_arrTDrls[i].DradleStoppedEvent += new DradleStoppedEventHandler(GameManager_DradleStoppedEvent);
            }
        }

        private void GameManager_DradleStoppedEvent(object sender, DradleStoppedEventArgs e)
        {
            // Check if the player was right
            if (PlayerGuess == e.LetterStoppedOn)
            {
                ++PlayerScore;
                SetScore(PlayerScore);
            }

            --m_nCurrSpinning;

            // That was the last dradle
            if (m_nCurrSpinning == 0)
            {
                IsGameOn = false;
                PlayerGuess = DradleLetters.ERROR;
            }
        }

        private void SetScore(int i_PlayerScore)
        {
            Game.Window.Title = "Score " + i_PlayerScore.ToString();
        }

        /// <summary>
        /// Update method for calcing the game
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            InputManager mgr = (InputManager)this.Game.Services.GetService(typeof(IInputManager));

            // Check that the dradels are not spinning
            if (!IsGameOn && (PlayerGuess != DradleLetters.ERROR))
            {
                // If the user pressed
                if (mgr.KeyReleased(Keys.Space))
                {
                    // Mini game starts!
                    IsGameOn = true;

                    foreach (Dradle drl in m_arrDrls)
                    {
                        drl.Rotate(((UtilityService)this.Game.Services.GetService(typeof(UtilityService))).RandomNumber(1, 7));
                    }

                    foreach (TexturedDradle drl in m_arrTDrls)
                    {
                        drl.Rotate(((UtilityService)this.Game.Services.GetService(typeof(UtilityService))).RandomNumber(1, 7));
                    }

                    // Save the number of dradels
                    m_nCurrSpinning = m_arrDrls.Length + m_arrTDrls.Length;
                }
            }
            else if (mgr.KeyReleased(Keys.B))
            {
                PlayerGuess = DradleLetters.N;
            }
            else if (mgr.KeyReleased(Keys.D))
            {
                PlayerGuess = DradleLetters.G;
            }
            else if (mgr.KeyReleased(Keys.V))
            {
                PlayerGuess = DradleLetters.H;
            }
            else if (mgr.KeyReleased(Keys.P))
            {
                PlayerGuess = DradleLetters.P;
            }
        }
        #endregion
    }
}
